package com.harmonyos.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmonyos.domain.TbPsychological;
import com.harmonyos.service.TbPsychologicalService;
import com.harmonyos.mapper.TbPsychologicalMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbPsychologicalServiceImpl extends ServiceImpl<TbPsychologicalMapper, TbPsychological>
implements TbPsychologicalService{
    @Resource
    private TbPsychologicalMapper tbPsychologicalMapper;

    @Override
    public String insert(TbPsychological tbPsychological) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbPsychologicalMapper.insert(tbPsychological);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String select(String user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbPsychological tbPsychological1 = JSONUtil.toBean(user, TbPsychological.class);
            TbPsychological tbPsychological = tbPsychologicalMapper.selectById(tbPsychological1.getSrc());
            objectObjectHashMap.put("data",tbPsychological);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectAll() {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            LambdaQueryWrapper<TbPsychological> wrapper = new LambdaQueryWrapper<>();
            List<TbPsychological> tbPsychologicals = tbPsychologicalMapper.selectList(wrapper);
            objectObjectHashMap.put("data",tbPsychologicals);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }


    public String update(TbPsychological tbPsychological){
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbPsychologicalMapper.updateById(tbPsychological);

            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String delect(String src) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbPsychological tbPsychological = JSONUtil.toBean(src, TbPsychological.class);
            tbPsychologicalMapper.deleteById(tbPsychological.getSrc());
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }
}




